﻿using EasyTest.Shared.Extensions;
using EasyTest.Shared.TestAttribute;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Web;


namespace ZLGCANoptimize
{
    public class 收音机
    {
        //static ZLGCANHelper zLGCANHelper = null;
        //static List<ZCAN_Receive_Data> zCAN_Receive_Datas = null;

        [ExposedMethodAttribute("")]
        public bool 切换FM音源(int 通道, out string 信息, out string data)
        {
#if ELO20240927
            data = "";

            try
            {
                if (!数据.初始化(通道, out ZLGCANHelper zLGCANHelper))
                {
                    信息 = "启动can设备失败";
                    return false;
                }
                _ = zLGCANHelper.RecvData(通道, 0x769, 1000, 100);
                bool OK = Can.解密(通道,new List<ZCAN_Receive_Data>(), out 信息);
                if (!OK)
                {
                    信息 = "解密失败";
                    return false;
                }
                //10 0C 2E 4C 10 02 00 00
                //21 00 00 00 00 00 00 00
                bool result1;
                result1 = zLGCANHelper.SentData(通道, 0x761, "04 2E 4C 10 02 00 00 00", true, SendType.正常发送);
                if (!result1)
                {
                    信息 = "发送数据失败,切换FM音源失败";
                    return false;
                }
                Thread.Sleep(50);
                List<ZCAN_Receive_Data> zCAN_Receive_Datas = zLGCANHelper.RecvData(通道, 0x769, 3, 500);
                string[] datas = ZLGCanTool.GetData(zCAN_Receive_Datas);// 03 6E 4C 10 AA AA AA AA
                data = string.Join("\n", datas);
                if (datas.Length > 0)
                {
                    foreach (string s in datas)
                    {
                        //03 6E 4C 10 AA AA AA AA
                        if (s.Trim().StartsWith("03 6E 4C 10"))
                        {
                            信息 = "切换FM音源成功";
                            return true;
                        }
                    }

                }

                信息 = "切换FM音源失败";
            }
            catch (Exception ex)
            {
                信息 = "切换FM音源失败" + ex.Message;
            }
            return false;
#else
            data = "";

            try
            {
                if (!数据.初始化(通道, out ZLGCANHelper zLGCANHelper))
                {
                    信息 = "启动can设备失败";
                    return false;
                }
                _ = zLGCANHelper.RecvData(通道, 0x769, 1000, 100);
                bool OK = Can.解密(通道, new List<ZCAN_Receive_Data>(), out 信息);
                if (!OK)
                {
                    信息 = "解密失败";
                    return false;
                }
                //10 0C 2E 4C 10 02 00 00
                // 21 00 00 00 00 00 00 00
                bool result1, result2;
                result1 = zLGCANHelper.SentData(通道, 0x761, "10 0C 2E 4C 10 02 00 00", true, SendType.正常发送);
                Thread.Sleep(50);
                result2 = zLGCANHelper.SentData(通道, 0x761, "21 00 00 00 00 00 00 00", true, SendType.正常发送);
                if (!result1)
                {
                    信息 = "发送数据失败,切换FM音源失败";
                    return false;
                }
                Thread.Sleep(50);
                List<ZCAN_Receive_Data> zCAN_Receive_Datas = zLGCANHelper.RecvData(通道, 0x769, 3, 500);
                string[] datas = ZLGCanTool.GetData(zCAN_Receive_Datas);// 03 6E 4C 10 AA AA AA AA
                data = string.Join("\n", datas);
                if (datas.Length > 0)
                {
                    foreach (string s in datas)
                    {
                        //03 6E 4C 10 AA AA AA AA
                        if (s.Trim().StartsWith("03 6E 4C 10"))
                        {
                            信息 = "切换FM音源成功";
                            return true;
                        }
                    }

                }

                //3、关闭
                信息 = "切换FM音源失败";
                return false;
            }
            catch (Exception ex)
            {
                信息 = "切换FM音源失败" + ex.Message;
                return false;
            }
#endif
        }

        [ExposedMethodAttribute("")]
        public bool 切换AM音源(int 通道, out string 信息, out string data)
        {
#if ELO20240927
            data = "";
            try
            {
                if (!数据.初始化(通道, out ZLGCANHelper zLGCANHelper))
                {
                    信息 = "启动can设备失败";
                    return false;
                }
                _ = zLGCANHelper.RecvData(通道, 0x769, 1000, 100);
                bool OK = Can.解密(通道,new List<ZCAN_Receive_Data>(), out 信息);
                if (!OK)
                {
                    信息 = "解密失败";
                    return false;
                }
                //10 0C 2E 4C 10 02 00 00
                // 21 00 00 00 00 00 00 00
                bool result1;
                result1 = zLGCANHelper.SentData(通道, 0x761, "04 2E 4C 10 01 00 00 00", true, SendType.正常发送);
                if (!result1)
                {
                    信息 = "发送数据失败,切换AM音源失败";
                    return false;
                }
                Thread.Sleep(50);
                List<ZCAN_Receive_Data> zCAN_Receive_Datas = zLGCANHelper.RecvData(通道, 0x769, 3, 500);
                string[] datas = ZLGCanTool.GetData(zCAN_Receive_Datas);// 03 6E 4C 10 AA AA AA AA
                data = string.Join("\n", datas);
                if (datas.Length > 0)
                {
                    foreach (string s in datas)
                    {
                        //03 6E 4C 10 AA AA AA AA
                        if (s.Trim().StartsWith("03 6E 4C 10"))
                        {
                            信息 = "切换AM音源成功";
                            return true;
                        }
                    }
                }
                //3、关闭
                信息 = "切换AM音源失败";
            }
            catch (Exception ex)
            {
                信息 = "切换AM音源失败" + ex.Message;
            }
            return false;
#else
            data = "";
            try
            {
                if (!数据.初始化(通道, out ZLGCANHelper zLGCANHelper))
                {
                    信息 = "启动can设备失败";
                    return false;
                }
                _ = zLGCANHelper.RecvData(通道, 0x769, 1000, 100);
                bool OK = Can.解密(通道, new List<ZCAN_Receive_Data>(), out 信息);
                if (!OK)
                {
                    信息 = "解密失败";
                    return false;
                }
                //10 0C 2E 4C 10 02 00 00
                // 21 00 00 00 00 00 00 00
                bool result1, result2;
                result1 = zLGCANHelper.SentData(通道, 0x761, "10 0C 2E 4C 10 01 00 00", true, SendType.正常发送);
                Thread.Sleep(50);
                result2 = zLGCANHelper.SentData(通道, 0x761, "21 00 00 00 00 00 00 00", true, SendType.正常发送);
                if (!result1)
                {
                    信息 = "发送数据失败,切换AM音源失败";
                    return false;
                }
                Thread.Sleep(50);
                List<ZCAN_Receive_Data> zCAN_Receive_Datas = zLGCANHelper.RecvData(通道, 0x769, 3, 500);
                string[] datas = ZLGCanTool.GetData(zCAN_Receive_Datas);// 03 6E 4C 10 AA AA AA AA
                data = string.Join("\n", datas);
                if (datas.Length > 0)
                {
                    foreach (string s in datas)
                    {
                        //03 6E 4C 10 AA AA AA AA
                        if (s.Trim().StartsWith("03 6E 4C 10"))
                        {
                            信息 = "切换AM音源成功";
                            return true;
                        }
                    }
                }
                //3、关闭
                信息 = "切换AM音源失败";
                return false;
            }
            catch (Exception ex)
            {
                信息 = "切换AM音源失败" + ex.Message;
                return false;
            }
#endif
        }

        [ExposedMethodAttribute("")]
        public bool 设置FM频率(int 通道, out string 信息, out string data, out string 发送, out int 读取频率, int 频率=106100)
        {
            读取频率 = 0;
            bool 设置ok = false;
            bool 读取ok = false;
            List<string> list = new List<string>()
            { "AA", "AA", "AA", "AA", "AA", "AA", "AA", "AA",
                "AA", "AA", "AA", "AA","AA", "AA", "AA", "AA"};
            data = "";
            string _频率 = 频率.ToString("d7");
            list.InsertRange(0, _频率.ToHexArray());
            string 首帧 = "10 0B 2E 4C A2 02 " + String.Join(" ", list.GetRange(0, 2)).Trim();
            string 帧21 = $"21 " + String.Join(" ", list.GetRange(2, 7)).Trim();
            发送 = 首帧 + "-" + 帧21;
            try
            {
                if (!数据.初始化(通道, out ZLGCANHelper zLGCANHelper))
                {
                    信息 = "启动can设备失败";
                    return false;
                }
                _ = zLGCANHelper.RecvData(通道, 0x769, 1000, 100);
                bool OK = Can.解密(通道, new List<ZCAN_Receive_Data>(), out 信息);
                if (!OK)
                {
                    信息 = "解密失败";
                    return false;
                }
                //String.Join(" ", list.GetRange(0, 2)).Trim()

                bool result1, result2;

                result1 = zLGCANHelper.SentData(通道, 0x761, 首帧, true, SendType.正常发送);
                Thread.Sleep(100);
                result2 = zLGCANHelper.SentData(通道, 0x761, 帧21, true, SendType.正常发送);
                if (!result1)
                {
                    信息 = "发送数据失败,设置FM频率失败";
                    return false;
                }
                List<ZCAN_Receive_Data> zCAN_Receive_Datas = zLGCANHelper.RecvData(通道, 0x769, 3, 500);

                string[] datas = ZLGCanTool.GetData(zCAN_Receive_Datas);// 03 6E 4C 10 AA AA AA AA

                if (datas.Length > 0)
                {
                    foreach (string s in datas)
                    {
                        //03 6E 4C 10 AA AA AA AA
                        if (s.Trim().StartsWith("03 6E 4C A2"))
                        {
                            信息 = "设置FM频率成功";
                            设置ok = true;
                        }
                    }
                }
                Thread.Sleep(100);
                string dataRead = "";
                for (int i = 0; i < 2; i++)
                {
                    FM频率(通道, zLGCANHelper, out 读取频率, out 信息, out dataRead);
                    if (读取频率 == 频率)
                    {
                        信息 = "设置FM频率成功";
                        读取ok = true;
                        break;
                    }
                }
                data = "设置报文:" + 首帧 + "\n" + 帧21 + "\n"
                    + "设置响应:" + string.Join("\n", datas) + "\n"
                    + "读取报文:" + dataRead + "\n"
                    + "当前频率:" + 读取频率;
                return 读取ok && 设置ok;
            }
            catch (Exception ex)
            {
                信息 = "设置FM频率失败" + ex.Message;
                return false;
            }
        }

      //  [ExposedMethodAttribute("")]
//        public bool 设置AM频率(int 通道, int 频率, out string 信息, out string data, out string 发送, out int 读取频率)
//        {
//#if ELO20240927
//            List<string> list = new List<string>()
//             { "AA", "AA", "AA", "AA", "AA", "AA", "AA", "AA",
//                "AA", "AA", "AA", "AA","AA", "AA", "AA", "AA"};
//            data = "";
//            读取频率 = 0;
//            bool 设置ok = false;
//            bool 读取ok = false;
//            string _频率 = 频率.ToString("d7");
//            list.InsertRange(0, _频率.ToHexArray());//10 13 2E F1 8C 20 32  33 
//            string 首帧 = "10 0B 2E 4C A2 01 " + String.Join(" ", list.GetRange(0, 2)).Trim();
//            string 帧21 = $"21 " + String.Join(" ", list.GetRange(2, 7)).Trim();
//            发送 = 首帧 + "-" + 帧21;
//            try
//            {
//                if (!数据.初始化(通道, out ZLGCANHelper zLGCANHelper))
//                {
//                    信息 = "启动can设备失败";
//                    return false;
//                }
//                _ = zLGCANHelper.RecvData(通道, 0x769, 1000, 100);
//                bool OK = Can.解密(通道,new List<ZCAN_Receive_Data>(), out 信息);
//                if (!OK)
//                {
//                    信息 = "解密失败";
//                    return false;
//                }
//                bool result1, result2;
//                result1 = zLGCANHelper.SentData(通道, 0x761, 首帧, true, SendType.正常发送);
//                Thread.Sleep(50);
//                result2 = zLGCANHelper.SentData(通道, 0x761, 帧21, true, SendType.正常发送);
//                if (!result1)
//                {
//                    信息 = "发送数据失败,设置AM频率失败";
//                    return false;
//                }
//                List<ZCAN_Receive_Data> zCAN_Receive_Datas = zLGCANHelper.RecvData(通道, 0x769, 3, 500);

//                string[] datas = ZLGCanTool.GetData(zCAN_Receive_Datas);// 03 6E 4C 10 AA AA AA AA
//                data = string.Join("\n", datas);
//                if (datas.Length > 0)
//                {
//                    foreach (string s in datas)
//                    {
//                        //03 6E 4C 10 AA AA AA AA
//                        if (s.Trim().StartsWith("03 6E 4C A2"))
//                        {
//                            信息 = "设置AM频率成功";
//                            设置ok = true;
//                        }
//                    }
//                }
//                string dataRead = "";
//                for (int i = 0; i < 2; i++)
//                {
//                    FM频率(通道, zLGCANHelper, out 读取频率, out 信息, out dataRead);
//                    if (读取频率 == 频率)
//                    {
//                        信息 = "读取AM频率成功";
//                        读取ok = true;
//                        break;
//                    }
//                }
//                data = "设置报文:" + 首帧 + "\n" + 帧21 + "\n"
//                    + "设置响应:" + string.Join("\n", datas) + "\n"
//                    + "读取报文:" + dataRead + "\n"
//                    + "当前频率:" + 读取频率;

//                return 读取ok & 设置ok;
//            }
//            catch (Exception ex)
//            {
//                信息 = "设置AM频率失败" + ex.Message;
//            }
//            return false;
//#else
//            List<string> list = new List<string>()
//             { "AA", "AA", "AA", "AA", "AA", "AA", "AA", "AA",
//                "AA", "AA", "AA", "AA","AA", "AA", "AA", "AA"};
//            data = "";
//            读取频率 = 0;
//            bool 设置ok = false;
//            bool 读取ok = false;
//            string _频率 = 频率.ToString("d7");
//            list.InsertRange(0, _频率.ToHexArray());//10 13 2E F1 8C 20 32  33 
//            string 首帧 = "10 0B 2E 4C A2 01 " + String.Join(" ", list.GetRange(0, 2)).Trim();
//            string 帧21 = $"21 " + String.Join(" ", list.GetRange(2, 7)).Trim();
//            发送 = 首帧 + "-" + 帧21;
//            try
//            {
//                if (!数据.初始化(通道, out ZLGCANHelper zLGCANHelper))
//                {
//                    信息 = "启动can设备失败";
//                    return false;
//                }
//                _ = zLGCANHelper.RecvData(通道, 0x769, 1000, 100);
//                bool OK = Can.解密(通道, new List<ZCAN_Receive_Data>(), out 信息);
//                if (!OK)
//                {
//                    信息 = "解密失败";
//                    return false;
//                }
//                bool result1, result2;
//                result1 = zLGCANHelper.SentData(通道, 0x761, 首帧, true, SendType.正常发送);
//                Thread.Sleep(50);
//                result2 = zLGCANHelper.SentData(通道, 0x761, 帧21, true, SendType.正常发送);
//                if (!result1)
//                {
//                    信息 = "发送数据失败,设置AM频率失败";
//                    return false;
//                }
//                List<ZCAN_Receive_Data> zCAN_Receive_Datas = zLGCANHelper.RecvData(通道, 0x769, 3, 500);

//                string[] datas = ZLGCanTool.GetData(zCAN_Receive_Datas);// 03 6E 4C 10 AA AA AA AA
//                data = string.Join("\n", datas);
//                if (datas.Length > 0)
//                {
//                    foreach (string s in datas)
//                    {
//                        //03 6E 4C 10 AA AA AA AA
//                        if (s.Trim().StartsWith("03 6E 4C A2"))
//                        {
//                            信息 = "设置AM频率成功";
//                            设置ok = true;
//                        }
//                    }
//                }
//                string dataRead = "";
//                for (int i = 0; i < 2; i++)
//                {
//                    FM频率(通道, zLGCANHelper, out 读取频率, out 信息, out dataRead);
//                    if (读取频率 == 频率)
//                    {
//                        信息 = "读取AM频率成功";
//                        读取ok = true;
//                        break;
//                    }
//                }
//                data = "设置报文:" + 首帧 + "\n" + 帧21 + "\n"
//                    + "设置响应:" + string.Join("\n", datas) + "\n"
//                    + "读取报文:" + dataRead + "\n"
//                    + "当前频率:" + 读取频率;
//                return 读取ok & 设置ok;
//            }
//            catch (Exception ex)
//            {
//                信息 = "设置AM频率失败" + ex.Message;
//                return false;
//            }
//#endif
//        }

        [ExposedMethodAttribute("")]
        public bool 读取频率(int 通道, out int 频率, out string 信息, out string data)
        {
            data = "";
            频率 = 0;
            try
            {
                if (!数据.初始化(通道, out ZLGCANHelper zLGCANHelper))
                {
                    信息 = "启动can设备失败";
                    return false;
                }
                _ = zLGCANHelper.RecvData(通道, 0x769, 1000, 100);
                bool OK = Can.解密(通道, new List<ZCAN_Receive_Data>(), out 信息);
                if (!OK)
                {
                    信息 = "解密失败";
                    return false;
                }
                return FM频率(通道, zLGCANHelper, out 频率, out 信息, out data);
            }
            catch (Exception ex)
            {
                信息 = "读取FM频率失败" + ex.Message;
                return false;
            }
        }

        public bool FM频率(int 通道, ZLGCANHelper zLGCANHelper, out int 频率, out string 信息, out string data)
        {
#if ELO20240927
            data = "";
            频率 = 0;
            try
            {
                _ = zLGCANHelper.RecvData(通道, 0x769, 10, 30);
                bool result1;
                Thread.Sleep(50);
                result1 = zLGCANHelper.SentData(通道, 0x761, "03 22 4C A2 00 00 00 00", true, SendType.正常发送);
                Thread.Sleep(80);
                zLGCANHelper.SentData(通道, 0x761, "30 08 14 00 00 00 00 00", true, SendType.正常发送);
                Thread.Sleep(80);
                if (!result1)
                {
                    信息 = "发送数据失败,读取FM频率失败";
                    return false;
                }
                List<ZCAN_Receive_Data> zCAN_Receive_Datas = zLGCANHelper.RecvData(通道, 0x769, 3, 1500);
                string[] datas = ZLGCanTool.GetData(zCAN_Receive_Datas);
                data = string.Join("\n", datas);
                string[] hexValues = new string[7];
                bool ok10 = false;
                bool ok21 = false;
                int num = 0;
                foreach (string s in datas)
                {
                    if (s.Trim().StartsWith("10"))
                    {
                        ok10 = s.Split(' ')[5] == "00";
                        for (int i = 0; i < s.Split(' ').Length; i++)
                        {
                            if (i <= 5 || i > 7) continue;
                            hexValues[num] = s.Split(' ')[i];
                            num++;
                        }
                    }
                    else if (s.Trim().StartsWith("21"))
                    {
                        ok21 = true;
                        for (int i = 0; i < s.Split(' ').Length; i++)
                        {
                            if (i == 0 || i > 5) continue;
                            hexValues[num] = s.Split(' ')[i];
                            num++;
                        }
                    }
                }
                List<byte> hexs = new List<byte>();
                foreach (var str in hexValues)
                {
                    byte hexValue = Convert.ToByte(str, 16);
                    hexs.Add(hexValue);
                }
                string str1 = Encoding.ASCII.GetString(hexs.ToArray());

                频率 = int.Parse(str1);
                信息 = string.Join(",", hexValues);

                return ok10 && ok21;
            }
            catch (Exception ex)
            {
                信息 = "读取FM频率失败" + ex.Message;
                return false;
            }
#else
            data = "";
            频率 = 0;
            try
            {
                _ = zLGCANHelper.RecvData(通道, 0x769, 10, 30);
                bool result1, result2;
                Thread.Sleep(50);
                result1 = zLGCANHelper.SentData(通道, 0x761, "03 22 4C A2 00 00 00 00", true, SendType.正常发送);
                Thread.Sleep(80);
                zLGCANHelper.SentData(通道, 0x761, "30 08 14 00 00 00 00 00", true, SendType.正常发送);
                if (!result1)
                {
                    信息 = "发送数据失败,读取FM频率失败";
                    return false;
                }
                //Thread.Sleep(200);
                List<ZCAN_Receive_Data> zCAN_Receive_Datas = zLGCANHelper.RecvData(通道, 0x769, 3, 1500);
                string[] datas = ZLGCanTool.GetData(zCAN_Receive_Datas);// 03 6E 4C 10 AA AA AA AA
                data = string.Join("\n", datas);
                string[] hexValues = new string[7];
                bool ok10 = false;
                bool ok21 = false;
                bool ok22 = false;
                int num = 0;
                foreach (string s in datas)
                {
                    if (s.Trim().StartsWith("10"))
                    {
                        ok10 = s.Split(' ')[5] == "00";
                        for (int i = 0; i < s.Split(' ').Length; i++)
                        {
                            if (i <= 5 || i > 7) continue;
                            hexValues[num] = s.Split(' ')[i];
                            num++;
                        }
                    }
                    else if (s.Trim().StartsWith("21"))
                    {
                        ok21 = true;
                        for (int i = 0; i < s.Split(' ').Length; i++)
                        {
                            if (i == 0 || i > 5) continue;
                            hexValues[num] = s.Split(' ')[i];
                            num++;
                        }
                    }
                }
                List<byte> hexs = new List<byte>();
                foreach (var str in hexValues)
                {

                    byte hexValue = Convert.ToByte(str, 16);
                    hexs.Add(hexValue);

                }
                string str1 = Encoding.ASCII.GetString(hexs.ToArray());

                频率 = int.Parse(str1);
                信息 = string.Join(",", hexValues);
                return ok10;
            }
            catch (Exception ex)
            {
                信息 = "读取FM频率失败" + ex.Message;
                return false;
            }
#endif
        }

        public bool AM频率(int 通道, ZLGCANHelper zLGCANHelper, out int 频率, out string 信息, out string data)
        {
#if ELO20240927
            data = "";
            频率 = 0;
            try
            {

                _ = zLGCANHelper.RecvData(通道, 0x769, 1000, 30);

                bool result1;
                result1 = zLGCANHelper.SentData(通道, 0x761, "03 22 4C A2 00 00 00 00", true, SendType.正常发送);
                zLGCANHelper.SentData(通道, 0x761, "30 08 14 00 00 00 00 00", true, SendType.正常发送);
                Thread.Sleep(100);
                Thread.Sleep(50);
                if (!result1)
                {
                    信息 = "发送数据失败,读取FM频率失败";
                    return false;
                }
                List<ZCAN_Receive_Data> zCAN_Receive_Datas = zLGCANHelper.RecvData(通道, 0x769, 3, 500);
                //10 0A 62 4C 8B 00 48 42              
                // 21 21 77 24 80 AA AA AA
                string[] datas = ZLGCanTool.GetData(zCAN_Receive_Datas);
                data = string.Join("\n", datas);
                string[] hexValues = new string[7];
                bool ok10 = false;
                bool ok21 = false;
                int num = 0;
                //10 OB 62 4C A2 00 30 30
                //21 39 38 31 30 30 AA AA
                foreach (string s in datas)
                {
                    if (s.Trim().StartsWith("10"))
                    {
                        ok10 = s.Split(' ')[5] == "00";
                        for (int i = 0; i < s.Split(' ').Length; i++)
                        {
                            if (i <= 5 || i > 7) continue;
                            hexValues[num] = s.Split(' ')[i];
                            num++;
                        }
                    }
                    else if (s.Trim().StartsWith("21"))
                    {
                        ok21 = true;
                        for (int i = 0; i < s.Split(' ').Length; i++)
                        {
                            if (i == 0 || i > 5) continue;
                            hexValues[num] = s.Split(' ')[i];
                            num++;
                        }
                    }
                }
                List<byte> hexs = new List<byte>();
                foreach (var str in hexValues)
                {
                    byte hexValue = Convert.ToByte(str, 16);
                    hexs.Add(hexValue);
                }

                string str1 = Encoding.ASCII.GetString(hexs.ToArray());

                频率 = int.Parse(str1);
                信息 = string.Join(",", hexValues) + "\n" + data + "\n" + 频率;
                return ok10 && ok21;
            }
            catch (Exception ex)
            {
                信息 = "读取FM频率失败" + ex.Message;
                return false;
            }
#else
            data = "";
            频率 = 0;
            try
            {

                _ = zLGCANHelper.RecvData(通道, 0x769, 1000, 30);

                bool result1, result2;
                result1 = zLGCANHelper.SentData(通道, 0x761, "03 22 4C A2 00 00 00 00", true, SendType.正常发送);
                Thread.Sleep(50);
                zLGCANHelper.SentData(通道, 0x761, "30 08 14 00 00 00 00 00", true, SendType.正常发送);
                Thread.Sleep(100);
                if (!result1)
                {
                    信息 = "发送数据失败,读取FM频率失败";
                    return false;
                }
                List<ZCAN_Receive_Data> zCAN_Receive_Datas = zLGCANHelper.RecvData(通道, 0x769, 3, 500);
                //10 0A 62 4C 8B 00 48 42              
                // 21 21 77 24 80 AA AA AA
                string[] datas = ZLGCanTool.GetData(zCAN_Receive_Datas);// 03 6E 4C 10 AA AA AA AA
                data = string.Join("\n", datas);
                string[] hexValues = new string[7];
                bool ok10 = false;
                bool ok21 = false;
                bool ok22 = false;
                int num = 0;
                //10 OB 62 4C A2 00 30 30
                //21 39 38 31 30 30 AA AA
                foreach (string s in datas)
                {
                    if (s.Trim().StartsWith("10"))
                    {
                        ok10 = s.Split(' ')[5] == "00";
                        for (int i = 0; i < s.Split(' ').Length; i++)
                        {
                            if (i <= 5 || i > 7) continue;
                            hexValues[num] = s.Split(' ')[i];
                            num++;
                        }
                    }
                    else if (s.Trim().StartsWith("21"))
                    {
                        ok21 = true;
                        for (int i = 0; i < s.Split(' ').Length; i++)
                        {
                            if (i == 0 || i > 5) continue;
                            hexValues[num] = s.Split(' ')[i];
                            num++;
                        }
                    }
                }
                List<byte> hexs = new List<byte>();
                foreach (var str in hexValues)
                {

                    byte hexValue = Convert.ToByte(str, 16);
                    hexs.Add(hexValue);

                }

                string str1 = Encoding.ASCII.GetString(hexs.ToArray());

                频率 = int.Parse(str1);
                信息 = string.Join(",", hexValues) + "\n" + data + "\n" + 频率;
                return ok10;
            }
            catch (Exception ex)
            {
                信息 = "读取FM频率失败" + ex.Message;
                return false;
            }
#endif
        }

        //=================================== 2024-09-27 ELO指令更新，以下方法为对应更新的内容 ==========================================
        [ExposedMethodAttribute("")]
        public bool 切换FM音源4C10(int 通道, out string 信息, out string data)
        {
            data = "";

            try
            {
                if (!数据.初始化(通道, out ZLGCANHelper zLGCANHelper))
                {
                    信息 = "启动can设备失败";
                    return false;
                }
                _ = zLGCANHelper.RecvData(通道, 0x769, 1000, 100);
                bool OK = Can.解密(通道, new List<ZCAN_Receive_Data>(), out 信息);
                if (!OK)
                {
                    信息 = "解密失败";
                    return false;
                }
                //10 0C 2E 4C 10 02 00 00
                // 21 00 00 00 00 00 00 00
                bool result1;
                result1 = zLGCANHelper.SentData(通道, 0x761, "04 2E 4C 10 02 00 00 00", true, SendType.正常发送);
                if (!result1)
                {
                    信息 = "发送数据失败,切换FM音源失败";
                    return false;
                }
                Thread.Sleep(50);
                List<ZCAN_Receive_Data> zCAN_Receive_Datas = zLGCANHelper.RecvData(通道, 0x769, 3, 500);
                string[] datas = ZLGCanTool.GetData(zCAN_Receive_Datas);// 03 6E 4C 10 AA AA AA AA
                data = string.Join("\n", datas);
                if (datas.Length > 0)
                {
                    foreach (string s in datas)
                    {
                        //03 6E 4C 10 AA AA AA AA
                        if (s.Trim().StartsWith("03 6E 4C 10"))
                        {
                            信息 = "切换FM音源成功";
                            return true;
                        }
                    }

                }

                信息 = "切换FM音源失败";
            }
            catch (Exception ex)
            {
                信息 = "切换FM音源失败" + ex.Message;
            }
            return false;
        }
        [ExposedMethodAttribute("")]
        public bool 切换AM音源4C10(int 通道, out string 信息, out string data)
        {
            data = "";
            try
            {
                if (!数据.初始化(通道, out ZLGCANHelper zLGCANHelper))
                {
                    信息 = "启动can设备失败";
                    return false;
                }
                _ = zLGCANHelper.RecvData(通道, 0x769, 1000, 100);
                bool OK = Can.解密(通道, new List<ZCAN_Receive_Data>(), out 信息);
                if (!OK)
                {
                    信息 = "解密失败";
                    return false;
                }
                //10 0C 2E 4C 10 02 00 00
                // 21 00 00 00 00 00 00 00
                bool result1;
                result1 = zLGCANHelper.SentData(通道, 0x761, "04 2E 4C 10 01 00 00 00", true, SendType.正常发送);
                if (!result1)
                {
                    信息 = "发送数据失败,切换AM音源失败";
                    return false;
                }
                Thread.Sleep(50);
                List<ZCAN_Receive_Data> zCAN_Receive_Datas = zLGCANHelper.RecvData(通道, 0x769, 3, 500);
                string[] datas = ZLGCanTool.GetData(zCAN_Receive_Datas);// 03 6E 4C 10 AA AA AA AA
                data = string.Join("\n", datas);
                if (datas.Length > 0)
                {
                    foreach (string s in datas)
                    {
                        //03 6E 4C 10 AA AA AA AA
                        if (s.Trim().StartsWith("03 6E 4C 10"))
                        {
                            信息 = "切换AM音源成功";
                            return true;
                        }
                    }
                }
                //3、关闭
                信息 = "切换AM音源失败";
            }
            catch (Exception ex)
            {
                信息 = "切换AM音源失败" + ex.Message;
            }
            return false;
        }
        [ExposedMethodAttribute("")]
        public bool 设置AM频率4CA2(int 通道,  out string 信息, out string data, out string 发送, out int 读取频率,int 频率= 540)
        {
            List<string> list = new List<string>()
            {
                "AA", "AA", "AA", "AA", "AA", "AA", "AA", "AA",
                "AA", "AA", "AA", "AA", "AA", "AA", "AA", "AA",
            };
            data = "";
            读取频率 = 0;
            bool 设置ok = false;
            bool 读取ok = false;
            string _频率 = 频率.ToString("d7");
            list.InsertRange(0, _频率.ToHexArray());
            string 首帧 = "10 0B 2E 4C A2 01 " + String.Join(" ", list.GetRange(0, 2)).Trim();
            string 帧21 = $"21 " + String.Join(" ", list.GetRange(2, 7)).Trim();
            发送 = 首帧 + "-" + 帧21;
            try
            {
                if (!数据.初始化(通道, out ZLGCANHelper zLGCANHelper))
                {
                    信息 = "启动can设备失败";
                    return false;
                }
                _ = zLGCANHelper.RecvData(通道, 0x769, 1000, 100);
                bool OK = Can.解密(通道, new List<ZCAN_Receive_Data>(), out 信息);
                if (!OK)
                {
                    信息 = "解密失败";
                    return false;
                }
                bool result1, result2;
                result1 = zLGCANHelper.SentData(通道, 0x761, 首帧, true, SendType.正常发送);
                Thread.Sleep(50);
                result2 = zLGCANHelper.SentData(通道, 0x761, 帧21, true, SendType.正常发送);
                if (!result1)
                {
                    信息 = "发送数据失败,设置AM频率失败";
                    return false;
                }
                List<ZCAN_Receive_Data> zCAN_Receive_Datas = zLGCANHelper.RecvData(通道, 0x769, 3, 500);

                string[] datas = ZLGCanTool.GetData(zCAN_Receive_Datas);// 03 6E 4C 10 AA AA AA AA
                data = string.Join("\n", datas);
                if (datas.Length > 0)
                {
                    foreach (string s in datas)
                    {
                        //03 6E 4C 10 AA AA AA AA
                        if (s.Trim().StartsWith("03 6E 4C A2"))
                        {
                            信息 = "设置AM频率成功";
                            设置ok = true;
                        }
                    }
                }
                string dataRead = "";
                for (int i = 0; i < 2; i++)
                {
                    FM频率(通道, zLGCANHelper, out 读取频率, out 信息, out dataRead);
                    if (读取频率 == 频率)
                    {
                        信息 = "读取AM频率成功";
                        读取ok = true;
                        break;
                    }
                }
                data = "设置报文:" + 首帧 + "\n" + 帧21 + "\n"
                    + "设置响应:" + string.Join("\n", datas) + "\n"
                    + "读取报文:" + dataRead + "\n"
                    + "当前频率:" + 读取频率;

                return 读取ok & 设置ok;
            }
            catch (Exception ex)
            {
                信息 = "设置AM频率失败" + ex.Message;
            }
            return false;
        }
    }
}
